package com.library.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.base.ApiResult;
import com.library.entity.BookUserEntity;
import com.library.entity.NewUsers;
import com.library.entity.ObpCommitteeEntity;
import com.library.entity.ResourceEntity;
import com.library.entity.vo.townshipUserStatisticsVo;
import com.library.mapper.BookUserMapper;
import com.library.service.*;
import com.library.util.MoJiCommonUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class informationStatisticsServiceImpl implements informationStatisticsService {
	@Autowired
	private BookUserService bookUserService;
	@Autowired
	private ObpCommitteeService obpCommitteeService;
	@Autowired(required = false)
	private BookUserMapper mapper;
	@Autowired
	private resourceStatisticsService resourceService;
	@Autowired
	private ResourceService Service;

	@Override
	public ApiResult registerList(BookUserEntity bookUser) {
		String name = MoJiCommonUtils.getUserName();
		bookUser.setStatus("1");
		LambdaQueryWrapper<BookUserEntity> wrapperBookUserEntity = Wrappers.lambdaQuery(bookUser);
		wrapperBookUserEntity.eq(BookUserEntity::getUserName, name);
		BookUserEntity bookUserEntity = bookUserService.getOne(wrapperBookUserEntity);
		LambdaQueryWrapper<ObpCommitteeEntity> queryWrapper = new LambdaQueryWrapper<>();
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (!bookUserEntity.getJurisdiction().equals("1")&&!bookUserEntity.getJurisdiction().equals("0")) {
				if (bookUserEntity.getJurisdiction().equals("2")) {
					queryWrapper.likeRight(ObpCommitteeEntity::getCommitteeCode, bookUserEntity.getCity());
				} else if (bookUserEntity.getJurisdiction().equals("3")) {
					queryWrapper.likeRight(ObpCommitteeEntity::getCommitteeCode, bookUserEntity.getDistrict());
				} else if (bookUserEntity.getJurisdiction().equals("4")) {
					queryWrapper.likeRight(ObpCommitteeEntity::getCommitteeCode, bookUserEntity.getVillage());
				} else if (bookUserEntity.getJurisdiction().equals("5")) {
					queryWrapper.likeRight(ObpCommitteeEntity::getCommitteeCode, bookUserEntity.getCommittee());
				} else {
					return ApiResult.failed("该用户无权限");
				}
			}

		}
		int bookCount = obpCommitteeService.count(queryWrapper);
		return ApiResult.ok(bookCount);
	}

	@Override
	public ApiResult peopleList(BookUserEntity bookUser) {
		String name = MoJiCommonUtils.getUserName();
		LambdaQueryWrapper<BookUserEntity> wrapperBookUserEntity = Wrappers.lambdaQuery();
		wrapperBookUserEntity.eq(BookUserEntity::getUserName, name);
		BookUserEntity bookUserEntity = bookUserService.getOne(wrapperBookUserEntity);
		bookUser.setStatus("1");
		LambdaQueryWrapper<BookUserEntity> queryWrapper = Wrappers.lambdaQuery(bookUser);
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (bookUserEntity.getJurisdiction().equals("0")) {
				queryWrapper.eq(BookUserEntity::getProvince, bookUserEntity.getProvince());
				queryWrapper.between(BookUserEntity::getJurisdiction, 0, 5);
			} else if (bookUserEntity.getJurisdiction().equals("1")) {//省份
				queryWrapper.eq(BookUserEntity::getProvince, bookUserEntity.getProvince());
				queryWrapper.between(BookUserEntity::getJurisdiction, 1, 5);
			} else if (bookUserEntity.getJurisdiction().equals("2")) {
				queryWrapper.eq(BookUserEntity::getCity, bookUserEntity.getCity());
				queryWrapper.between(BookUserEntity::getJurisdiction, 2, 5);
			} else if (bookUserEntity.getJurisdiction().equals("3")) {
				queryWrapper.eq(BookUserEntity::getDistrict, bookUserEntity.getDistrict());
				queryWrapper.between(BookUserEntity::getJurisdiction, 3, 5);
			} else if (bookUserEntity.getJurisdiction().equals("4")) {
				queryWrapper.eq(BookUserEntity::getVillage, bookUserEntity.getVillage());
				queryWrapper.between(BookUserEntity::getJurisdiction, 4, 5);
			} else if (bookUserEntity.getJurisdiction().equals("5")) {
				queryWrapper.eq(BookUserEntity::getCommittee, bookUserEntity.getCommittee());
				queryWrapper.between(BookUserEntity::getJurisdiction, 5, 5);
			} else {
				return ApiResult.failed("该用户无权限");
			}
		}
		long count = bookUserService.count(queryWrapper);
		return ApiResult.ok(count);
	}

	@Override
	public ApiResult browseList() {
		Map<String, Integer> map = new HashMap<>();
		map.put("day", 365);
		map.put("month", 12);
		map.put("sum", 10250);
		return ApiResult.ok(map);
	}

	@Override
	public ApiResult totalUsers(BookUserEntity bookUser) {
		String name = MoJiCommonUtils.getUserName();
		LambdaQueryWrapper<BookUserEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(BookUserEntity::getUserName, name);
		BookUserEntity bookUserEntity = bookUserService.getOne(wrapper);
		bookUser.setStatus("1");
		LambdaQueryWrapper<BookUserEntity> queryWrapper = Wrappers.lambdaQuery(bookUser);
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (bookUserEntity.getJurisdiction().equals("1")||bookUserEntity.getJurisdiction().equals("0")) {//省份
				bookUser.setProvince(bookUserEntity.getProvince());
			} else if (bookUserEntity.getJurisdiction().equals("2")) {
				bookUser.setCity(bookUserEntity.getCity());
			} else if (bookUserEntity.getJurisdiction().equals("3")) {
				bookUser.setDistrict(bookUserEntity.getDistrict());
			} else if (bookUserEntity.getJurisdiction().equals("4")) {
				bookUser.setVillage(bookUserEntity.getVillage());
			} else if (bookUserEntity.getJurisdiction().equals("5")) {
				bookUser.setCommittee(bookUserEntity.getCommittee());
			} else {
				return ApiResult.failed("该用户无权限");
			}
		}

		long count = bookUserService.count(queryWrapper);
		return ApiResult.ok(count);
	}


	public long value(String getDataTypeId){
		LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ResourceEntity::getDataTypeId, getDataTypeId);
		return Service.count(wrapper);
	}

	@Override
	public ApiResult resourceAnalysis() {
		List<Map> list = new ArrayList<>();

		long value = value("1719628518976913409");
		Map map = new HashMap();
		map.put("name", "图书");
		map.put("value", value);

		long value1 = value("1719628549662441474");
		Map map1 = new HashMap();
		map1.put("name", "音频");
		map1.put("value", value1);

		long value2 = value("1719628572391374849");
		Map map2 = new HashMap();
		map2.put("name", "视频");
		map2.put("value", value2);

		long value3 = value("1719628593258037250");
		Map map3 = new HashMap();
		map3.put("name", "期刊");
		map3.put("value", value3);

		list.add(map1);
		list.add(map);
		list.add(map2);
		list.add(map3);
		return ApiResult.ok(list);
	}

	@Override
	public ApiResult accessSource() {
		List<Map> list = new ArrayList<>();
		Map map = new HashMap();
		map.put("name", "PC");
		map.put("value", "84");


		Map map1 = new HashMap();
		map1.put("name", "HTML5");
		map1.put("value", "120");


		Map map2 = new HashMap();
		map2.put("name", "小程序");
		map2.put("value", "130");


		Map map3 = new HashMap();
		map3.put("name", "ANDROID");
		map3.put("value", "749");

		Map map4 = new HashMap();
		map4.put("name", "IOS");
		map4.put("value", "1");

		list.add(map1);
		list.add(map);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		return ApiResult.ok(list);
	}

	@Override
	public ApiResult getNewUsers(String startTime, String endTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

		List<String> dateList = new ArrayList<>();
		if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
			LocalDate startDate = LocalDate.parse(startTime);
			LocalDate endDate = LocalDate.parse(endTime);
			while (!startDate.isAfter(endDate)) {
				dateList.add(startDate.format(formatter));
				startDate = startDate.plusDays(1);
			}
		} else if (StrUtil.isNotBlank(startTime)) {
			LocalDate startDate = LocalDate.parse(startTime);
			LocalDate endDate = LocalDate.now();
			while (!startDate.isAfter(endDate)) {
				dateList.add(startDate.format(formatter));
				startDate = startDate.plusDays(1);
			}
		} else if (StrUtil.isNotBlank(endTime)) {
			Date date = null;
			try {
				date = sdf.parse(endTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			for (int i = 0; i < 8; i++) {
				Date newdate = DateUtils.addDays(date, -i);
				String formatDate = sdf.format(newdate);
				dateList.add(formatDate);
			}
		} else {
			//获取近七天日期
			Date date = new Date();
			for (int i = 0; i < 8; i++) {
				Date newdate = DateUtils.addDays(date, -i);
				String formatDate = sdf.format(newdate);
				dateList.add(formatDate);
			}
		}
		String location = null;
		String name = MoJiCommonUtils.getUserName();
		LambdaQueryWrapper<BookUserEntity> userwrapper = Wrappers.lambdaQuery();
		userwrapper.eq(BookUserEntity::getUserName, name);
		BookUserEntity bookUserEntity = bookUserService.getOne(userwrapper);
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (bookUserEntity.getJurisdiction().equals("1")||bookUserEntity.getJurisdiction().equals("0")) {
				location = bookUserEntity.getProvince();
			} else if (bookUserEntity.getJurisdiction().equals("2")) {
				location = bookUserEntity.getCity();
			} else if (bookUserEntity.getJurisdiction().equals("3")) {
				location = bookUserEntity.getDistrict();
			} else if (bookUserEntity.getJurisdiction().equals("4")) {
				location = bookUserEntity.getVillage();
			} else if (bookUserEntity.getJurisdiction().equals("5")) {
				location = bookUserEntity.getCommittee();
			} else {
				return ApiResult.failed("没有权限");
			}
		} else {
			return ApiResult.failed("没有权限");
		}

		List<NewUsers> newUsersList = mapper.getNewUsers(dateList, Integer.parseInt(bookUserEntity.getJurisdiction()), location);
		List<NewUsers> list = new ArrayList<>();
		//外层七天循环  循环七次
		for (int i = 0; i < dateList.size(); i++) {
			//创建封装类对象  循环七次 创建七个
			NewUsers newUsers = new NewUsers();
			//将近七天日期添加进封装类中
			newUsers.setCreateTime(dateList.get(i));
			boolean bool = false;
			//创建内循环 根据查询出已有的数量 循环次数
			for (int m = 0; m < newUsersList.size(); m++) {
				if (dateList.get(i).equals(newUsersList.get(m).getCreateTime())) {
					newUsers.setNewUsers(newUsersList.get(m).getNewUsers());
					bool = true;
					break;
				}
			}
			if (!bool) {
				newUsers.setNewUsers("0");
			}
			list.add(newUsers);
		}
		return ApiResult.ok(list);
	}

	@Override
	public List<townshipUserStatisticsVo> townshipUserStatistics(Page page, townshipUserStatisticsVo townshipUserStatisticsVo) {
		String username = MoJiCommonUtils.getUserName();
		LambdaQueryWrapper<BookUserEntity> userwrapper = Wrappers.lambdaQuery();
		userwrapper.eq(BookUserEntity::getUserName, username);
		BookUserEntity bookUserEntity = bookUserService.getOne(userwrapper);
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (bookUserEntity.getJurisdiction().equals("1")||bookUserEntity.getJurisdiction().equals("0")) {
				townshipUserStatisticsVo.setCode(bookUserEntity.getProvince());
			} else if (bookUserEntity.getJurisdiction().equals("2")) {
				townshipUserStatisticsVo.setCode(bookUserEntity.getCity());
			} else if (bookUserEntity.getJurisdiction().equals("3")) {
				townshipUserStatisticsVo.setCode(bookUserEntity.getDistrict());
			} else if (bookUserEntity.getJurisdiction().equals("4")) {
				townshipUserStatisticsVo.setCode(bookUserEntity.getVillage());
			} else if (bookUserEntity.getJurisdiction().equals("5")) {
				townshipUserStatisticsVo.setCode(bookUserEntity.getCommittee());
			} else {
				return null;
			}
		} else {
			return null;
		}

		return mapper.townshipUserStatistics(page, townshipUserStatisticsVo, Integer.parseInt(bookUserEntity.getJurisdiction()));
	}

	@Override
	public ApiResult numberPeople(Page page, String name) {
		String location = null;
		String username = MoJiCommonUtils.getUserName();
		LambdaQueryWrapper<BookUserEntity> userwrapper = Wrappers.lambdaQuery();
		userwrapper.eq(BookUserEntity::getUserName, username);
		BookUserEntity bookUserEntity = bookUserService.getOne(userwrapper);
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (!bookUserEntity.getJurisdiction().equals("0")) {
				if (bookUserEntity.getJurisdiction().equals("1")) {
					location = bookUserEntity.getProvince();
				} else if (bookUserEntity.getJurisdiction().equals("2")) {
					location = bookUserEntity.getCity();
				} else if (bookUserEntity.getJurisdiction().equals("3")) {
					location = bookUserEntity.getDistrict();
				} else if (bookUserEntity.getJurisdiction().equals("4")) {
					location = bookUserEntity.getVillage();
				} else if (bookUserEntity.getJurisdiction().equals("5")) {
					location = bookUserEntity.getCommittee();
				} else {
					return ApiResult.failed("没有权限");
				}
			}
		} else {
			return ApiResult.failed("没有权限");
		}

		List<Map<Integer, String>> maps = mapper.numberPeople(Integer.parseInt(bookUserEntity.getJurisdiction()), location, page, name);

		return ApiResult.ok(page.setRecords(maps));
	}

	@Override
	public ApiResult activeArea(Page page, String name) {
		List<Map> list = new ArrayList<>();
		Map map = new HashMap();
		Map map1 = new HashMap();
		Map map2 = new HashMap();
		if (StrUtil.isNotBlank(name)) {
			if (name.equals("郑州市")) {
				map.put("name", "郑州市");
				map.put("value", "20");
				list.add(map);
			} else if (name.equals("鹤壁市")) {
				map.put("name", "鹤壁市");
				map.put("value", "30");
				list.add(map);
			} else if (name.equals("安阳市")) {
				map.put("name", "安阳市");
				map.put("value", "40");
				list.add(map);
			}
		} else {
			map.put("name", "郑州市");
			map.put("value", "20");
			map1.put("name", "鹤壁市");
			map1.put("value", "30");
			map2.put("name", "安阳市");
			map2.put("value", "40");

			list.add(map1);
			list.add(map);
			list.add(map2);
		}
		return ApiResult.ok(page.setRecords(list));
	}

	@Override
	public Map Statistics() {
		Map map = new HashMap();

		BookUserEntity bookUser = new BookUserEntity();
		ApiResult totalUsers = totalUsers(bookUser);
		map.put("totalUsers",totalUsers.getData());
		ApiResult browseList = browseList();
		map.put("browseList",browseList.getData());
		ApiResult resourceAnalysis = resourceAnalysis();
		map.put("resourceAnalysis",resourceAnalysis.getData());
		ApiResult accessSource = accessSource();
		map.put("accessSource",accessSource.getData());
		ApiResult newUsers = getNewUsers(null, null);
		map.put("newUsers",newUsers.getData());
		List<Map> audioRanking = resourceService.audioRanking();
		map.put("audioRanking",audioRanking);
		List<Map> bookRanking = resourceService.bookRanking();
		map.put("bookRanking",bookRanking);
		List<Map> periodicalRanking = resourceService.periodicalRanking();
		map.put("periodicalRanking",periodicalRanking);
		List<Map> videoRanking = resourceService.videoRanking();
		map.put("videoRanking",videoRanking);
		return map;
	}

}
